package com.beerq.util;

import android.app.Activity;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.beerq.R;

import java.util.List;

/**
 * 作者：minj on 2017/9/29 19:58
 * viewpaper的一些管理类
 */
public class ViewPaperUtil {

    private List<View> mListViews;
    private View mVTabLine;
    private ViewPager mViewPager;

    private boolean isScroll=false;


    public ViewPaperUtil(Activity context, ViewPager viewPager){
        this(context,viewPager,true);
    }

    public ViewPaperUtil(Activity context, ViewPager viewPager,boolean isCalculte){
        mViewPager=viewPager;
        mVTabLine=context.findViewById(R.id.v_tab_line);



        isScroll=isCalculte;

        if(isCalculte){
            initTabLine();
            calTabLinePosition(0);
        }

    }



    public void bind(List<View> listViews,final IGetCurrentSelectIndex iGetCurrentSelectIndex){
        mListViews=listViews;
        mViewPager.setAdapter(new MyPagerAdapter());
            mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageSelected(int pos) {
                    if(isScroll){
                        calTabLinePosition(pos);
                    }
                    if (iGetCurrentSelectIndex != null) {
                        iGetCurrentSelectIndex.getSelectIndex(pos);
                    }
                }

                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });


    }

    private void initTabLine() {
        LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mVTabLine.getLayoutParams();
        lp.width =AppConfig.deviceDensity*40;
        mVTabLine.setLayoutParams(lp);
    }




    //计算下划线的宽度
    private void calTabLinePosition(int pos){
        LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mVTabLine.getLayoutParams();
        lp.leftMargin =AppConfig.devicePixelsWidth/5*pos+AppConfig.devicePixelsWidth/10-lp.width/2;

        if(pos==2){
            mVTabLine.setBackgroundColor(0xff927551);
        }else{
            mVTabLine.setBackgroundColor(0xffffffff);
        }

        mVTabLine.setLayoutParams(lp);
    }


    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            Log.d("k", "destroyItem");
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }

        @Override
        public void finishUpdate(View arg0) {
            Log.d("k", "finishUpdate");
        }

        @Override
        public int getCount() {
            Log.d("k", "getCount");
            return mListViews.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            Log.d("k", "instantiateItem");
            ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
            return mListViews.get(arg1);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            Log.d("k", "isViewFromObject");
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            Log.d("k", "restoreState");
        }

        @Override
        public Parcelable saveState() {
            Log.d("k", "saveState");
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            Log.d("k", "startUpdate");
        }

    }

    public interface IGetCurrentSelectIndex{
        public void getSelectIndex(int pos);
    }

}
